function [adists, assignments, bassigned] = AssignUniqueObjDists(nobjsa, nobjsb, xs, ys, ex0s, ey0s)

%Dists between objects in previous frame (which has nobjsa and next frame
%which has nobjsb)
dists = zeros(nobjsb, nobjsa);
      
assignments = zeros(nobjsb, 1);
adists = zeros(nobjsb, 1);

x0a = xs;
y0a = ys;

x0b = ex0s;
y0b = ey0s;

maxval = 10000;
                
for j = 1:nobjsb
    for k = 1:nobjsa        
        dx = x0b(j) - x0a(k);
        dy = y0b(j) - y0a(k);
        
        dists(j,k) = sqrt(dx^2+dy^2);        
    end
end

diststemp = dists;

   
nassign = nobjsb;
assignments = zeros(nassign, 1);



for i = 1:nassign
    
     
    dmin = maxval;
    
    for j = 1:nobjsb
       [d, index] = min(dists(j,:));
            
        if d < dmin
           dmin = d;
           jassign = j;
           iassign = index;        
        end
    end        
    
    if dmin < maxval
       adists(jassign) = dmin;
       assignments(jassign) = iassign;
       dists(:,iassign) = maxval;
       dists(jassign,:) = maxval;
    
    end                                
end 

bassigned = 1;

if find(assignments == 0) > 0
    bassigned = 0;
    dists = diststemp;
    
    for j = 1:nobjsb      
        [d, index] = min(dists(j,:));
    
        adists(j) = d;
        assignments(j) = index;    
    end
end